package com.training1.core.web.controller;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.training1.core.common.controller.BaseController;
import com.training1.core.common.dao.Page;
import com.training1.core.common.entity.RespJson;
import com.training1.core.common.utils.framework.RespJsonFactory;
import com.training1.core.common.utils.security.MD5;
import com.training1.core.entity.SystemUserEntity;
import com.training1.core.modules.service.SystemUserService;

@Controller
@RequestMapping("systemusersetting")
public class SystemUserController extends BaseController{
	@Resource
	private SystemUserService systemUserService;
	
	/**
	 * 进入帮助中心列表页面
	 * @return
	 */
	@RequestMapping(value={"/",""})
	public String go(){
		return "system/systemUserSetting";
	}
	
	/**
	 * 新增编辑页面
	 * @param model
	 * @param id 如果存在，则为编辑
	 * @return
	 */
	@RequestMapping("edit")
	@ResponseBody
	public SystemUserEntity edit(Integer id, ModelMap model){
		
			SystemUserEntity systemUser = systemUserService.get(id);
			systemUser.setPassword(null);
			return systemUser;
		
	}
	
	/**
	 * 查询及列表往页面传值
	 * @param titleLike
	 * @param keyWord
	 * @param isValid
	 * @param page
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public Page<SystemUserEntity> list(String userName, String userAccount, String isValid, Page<SystemUserEntity> page){
		page.getSearch().put("userName", userName);
		page.getSearch().put("userAccount", userAccount);
		page.getSearch().put("validFlag", isValid);
		systemUserService.findList(page.getSearch());
		return page;
	}
	
	/**
	 * 新增编辑保存
	 * @param help
	 * @param result
	 * @return
	 */
	@RequestMapping(value="save", method={RequestMethod.POST})
	@ResponseBody
	public RespJson save(@Valid SystemUserEntity systemUser, BindingResult result){
		if(result.hasErrors()){
			return RespJsonFactory.buildFailure(result.getAllErrors().get(0).getDefaultMessage(), "model.help.save.error");
		}
		if(systemUser.getId()==null){
			//判断用户是否存在
			if(systemUserService.isUserExist(systemUser)){
				return RespJsonFactory.buildFailure("module.userId.exist");
			}
			//密码无效
//			if(StringUtils.isBlank(user.getPassword())){
//				return RespJsonFactory.buildFailure("module.user.password.error");
//			}
		}
		if(systemUser.getPassword()!=null){
			systemUser.setPassword(MD5.getMD5(systemUser.getPassword()));
		}
		systemUserService.save(systemUser);
		return RespJsonFactory.buildSuccess();
	} 
	
	/**
	 * 删除
	 * @param systemUser
	 * @return
	 */
	@RequestMapping(value="delete", method={RequestMethod.DELETE})
	@ResponseBody
	public RespJson delete(@RequestBody SystemUserEntity systemUser){
		systemUserService.delete(systemUser.getId());
		return RespJsonFactory.buildSuccess();
	}
}
